//
//  UUID.m
//  GHYunRui
//
//  Created by Hello Cai on 2019/7/24.
//  Copyright © 2019 Hello Cai. All rights reserved.
//

#import "UUID.h"
#import "KeyChainStore.h"

#define  KEY_IN_KEYCHAIN @"com.company.app.keychain"

@implementation UUID

+(void)saveUUID:(NSString *)uuid{
    if (uuid && uuid.length > 0) {
        [KeyChainStore save:KEY_IN_KEYCHAIN data:uuid];
    }
}

+(NSString *)getUUID{
    NSString * strUUID = (NSString *)[KeyChainStore load:KEY_IN_KEYCHAIN];
    
    //首次执行该方法时，uuid为空
    if ([strUUID isEqualToString:@""] || !strUUID){
        //生成一个uuid的方法
        CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
        strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
        //将该uuid保存到keychain
        [self saveUUID:strUUID];
        CFRelease(uuidRef);
    }
    return strUUID;
}

+(void)deleteUUID{
    [KeyChainStore deleteKeyData:KEY_IN_KEYCHAIN];
}

@end
